package demo; /**
 * @author tongchen
 * @create 2022-11-29 10:53
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/req5")
public class Servlet4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.解决post乱码
        request.setCharacterEncoding("UTF-8");


        //设置输入和输出
        String name = request.getParameter("username");
        //先输出处理乱码前的数据
        System.out.println(name);
        //在输出前解决乱码问题:先将Tomcat编码格式下的返回值转化为字节码形式，通过字节码进行解码获得原始数据
        String s = new String(name.getBytes("ISO_8859_1"), "UTF-8");

        System.out.println(s);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
